require 'esi/logger'

module ESI
  module Tag
    class Try < Base
      include ESI::Log
      
      def close( output )
        super(output)
        begin
          attempt = @children.detect{|tag| tag.name == "attempt" }
          attempt.children.each do|tag|
            tag.close( output, :raise => true )
          end
        rescue => e
          log_error e.message
          except = @children.detect{|tag| tag.name == "except" }
          except.children.each_with_index do|tag,index|
            output << except.buffers[index]
            tag.close( output )
          end
          output << except.buffers[except.children.size]
        end
      end

      def buffer( output, inner_html )
        @unclosed.buffer( output, inner_html ) if @unclosed
      end

    end
  end
end
